package com.mbbb.yygh.hosp.controller.api;

import com.mbbb.yygh.common.exception.YyghException;
import com.mbbb.yygh.common.result.R;
import com.mbbb.yygh.common.result.Result;
import com.mbbb.yygh.hosp.service.DepartmentService;
import com.mbbb.yygh.hosp.service.HospitalService;
import com.mbbb.yygh.hosp.service.HospitalSetService;
import com.mbbb.yygh.hosp.service.ScheduleService;
import com.mbbb.yygh.hosp.utils.HttpRequestHelper;
import com.mbbb.yygh.hosp.utils.MD5;
import com.mbbb.yygh.model.hosp.Department;
import com.mbbb.yygh.model.hosp.Hospital;
import com.mbbb.yygh.model.hosp.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {

        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String) stringObjectMap.get("hoscode");
        String hosScheduleId = (String) stringObjectMap.get("hosScheduleId");

        scheduleService.deleteSchedule(hoscode,hosScheduleId);

        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {

        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());

        int page = StringUtils.isEmpty(stringObjectMap.get("page")) ? 1 : Integer.parseInt((String)stringObjectMap.get("page"));
        int limit = StringUtils.isEmpty(stringObjectMap.get("limit")) ? 10 : Integer.parseInt((String)stringObjectMap.get("limit"));

        Page<Schedule> pages = scheduleService.selectSchedulePage(page,limit);

        return Result.ok(pages);
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {

        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());

        scheduleService.saveSchedule(stringObjectMap);

        return Result.ok();
    }

    @ApiOperation("删除科室")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) stringObjectMap.get("hoscode");
        String depcode = (String) stringObjectMap.get("depcode");

        departmentService.deleteDept(hoscode,depcode);

        return Result.ok();
    }

    @ApiOperation("获取分页列表")
    @PostMapping("/department/list")
    public Result department(HttpServletRequest request) {

        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());

        int page = StringUtils.isEmpty(stringObjectMap.get("page")) ? 1 : Integer.parseInt((String)stringObjectMap.get("page"));
        int limit = StringUtils.isEmpty(stringObjectMap.get("limit")) ? 10 : Integer.parseInt((String)stringObjectMap.get("limit"));

        Page<Department> pages = departmentService.selectPageDept(page,limit);
        return Result.ok(pages);
    }

    @ApiOperation("上传科室")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {

        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        departmentService.saveDepartment(stringObjectMap);

        return Result.ok();
    }

    @ApiOperation("获取医院信息")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取医院系统传递的数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode = (String) stringObjectMap.get("hoscode");
        //调用方法
        Hospital hospital = hospitalService.getHospHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //1.获取医院系统传过来的医院的map集合数据
        //String hoscode = request.getParameter("hoscode");
        Map<String, String[]> parameterMap = request.getParameterMap();
        // Map<String, String[]> -->  Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        //2.签名校验
        //① 获取医院系统传递过来的签名,签名进行MD5加密
        String sign = (String) stringObjectMap.get("sign");
        //② 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) stringObjectMap.get("hoscode");
        String sign_key = hospitalSetService.getSign_Key(hoscode);
        //③ 进行MD5加密
        String sign_keyMD5 = MD5.encrypt(sign_key);
        //④ 对比
        if (!sign.equals(sign_keyMD5)) {
            throw new YyghException(20001,"数据校验失败！");
        }

        //3.传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) stringObjectMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        stringObjectMap.put("logoData",logoData);

        //4.把map集合数据添加到mongodb数据库中
        hospitalService.saveHosp(stringObjectMap);
        return Result.ok();
    }
}